<template>
  <div>
    <div class="bg">
      <div class="adio">
        <div>
          我的总积分 <br />
          <h4>{{ jobs.cont }}</h4>
        </div>
      </div>
      <div id="pos" @click="integralMall">兑换好礼</div>
      <div class="grade">
        <div style="color: red;">
          <van-icon name="smile" size="1.7rem"/> 扣除积分 <div> {{ jobs.del }}</div>
        </div>
        <div  style="color: #FFC300;">
          <van-icon name="smile" size="1.7rem" /> 添加积分 <div style="color: #FFC300;"> {{ jobs.add }}</div>
        </div>
      </div>
      <div id="details">
        <div class="title">积分明细</div>
        <div class="list">  
          <div v-for="(item, index) in jobs.list" :key="index" class="item">
            <div>{{ item.date }}</div>
            <div>{{ item.record }}</div>
            <div v-if="item.status == 0" class="red-text">-{{ item.number }}</div>
            <div v-if="item.status == 1" class="black-text">+{{ item.number }}</div>
          </div>
          <van-empty style="" description="暂无明细" v-if="!loading && !jobs.list.length" />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { useUserInfoStore } from '@/stores/user.js' // 导入名为useUserInfoStore的自定义钩子函数，用于获取用户信息的Store对象
import { storeToRefs } from 'pinia' // 导入storeToRefs函数，用于将Store对象的属性转换为响应式数据
import { reactive, ref } from 'vue' // 导入reactive和ref函数，用于创建响应式数据
import { assignmentIntegralList } from '../api' // 导入assignmentIntegralList接口函数，用于获取积分任务列表数据
import { useRouter } from 'vue-router' // 导入useRouter函数，用于进行路由操作

const router = useRouter() // 创建名为router的Vue Router实例对象
const userInfoStore = useUserInfoStore() // 调用useUserInfoStore钩子函数创建名为userInfoStore的用户信息Store对象
const { curStudent } = storeToRefs(userInfoStore) // 使用storeToRefs将userInfoStore的curStudent属性转换为响应式数据，并赋值给curStudent变量
const loading = ref(true) // 创建名为loading的响应式变量，初始值为true
const jobs = reactive({ // 使用reactive函数创建名为jobs的响应式对象
  add: 0, // 增加的任务数，默认值为0
  del: 0, // 删除的任务数，默认值为0
  cont: 0, // 目前的任务数，默认值为0
  list: [] // 任务列表，默认为空数组
})

;(async function () { // 创建一个立即执行的异步函数，用于获取积分任务列表数据
  try {
    loading.value = true // 将loading的值设置为true，表示正在加载中
    const res = await assignmentIntegralList({ user_id: curStudent.value.id }) // 调用assignmentIntegralList接口函数，传递user_id参数，并等待返回结果
    Object.assign(jobs, { // 使用Object.assign方法将res的属性分配给jobs对象
      ...res,
      list: res.list.map((item) => { // 对list属性进行进一步处理，将日期格式化
        let date = new Date(item.date)
        item.date = `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}`
        return item
      })
    })
    loading.value = false // 将loading的值设置为false，表示加载完成
  } catch (error) {
    loading.value = false // 异常情况下，将loading的值设置为false
  }
})()

function integralMall() { // 定义名为integralMall的函数，用于跳转到积分商城页面
  router.push('/IntegralMall') // 通过router进行页面跳转，跳转至'/IntegralMall'路径
}

</script>

<style lang="less" scoped>
@var1: #fce79a;
@var2: #881074;
@var3: #ff4827;
@var4: #ffc300;
@var5: #eeeeee;
@var6: #ff4d32;
@var7: #ffffff;
@var8: #fff5f3;

.red-text {
	color: red;
	font-weight: 900;
}
.black-text {
	color: black;
	font-weight: 900;
}
.bg {
	background: -webkit-linear-gradient(top, #ffc456, #ffc400, @var7);
	height: 60vh;
	display: flex;
	justify-content: center;
	padding-top: 5vh;
	position: relative;
}
.adio {
	width: 150px;
	height: 150px;
	border-radius: 50%;
	color: white;
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	justify-content: center;
	text-align: center;
	font-size: 14px;
	font-weight: 900;
	border: 7px solid @var1;
	h4 {
		margin-top: 0.5vh;
		font-size: 2.2em;
		text-shadow: 2px 1px 2px @var2;
	}
}
#pos {
	position: absolute;
	top: 160px;
	background-color: @var3;
	color: white;
	font-weight: 900;
	font-size: 15px;
	padding: 1vh 8vw;
	border-radius: 20px;
}
.grade {
	position: absolute;
	top: 220px;
	display: flex;
	background-color: white;
	padding: 1.5vh 2vw;
	border-radius: 20px;
	width: 90%;
	justify-content: space-around;
	font-size: 14px;
	font-weight: 900;
	> div:first-child {
		color: @var4;
		// border-right: 1px solid @var5;
		padding-right: 12vw;
		display: flex;
		align-items: center;
	}
	> div:last-child {
		color: @var6;
		
		display: flex;
		align-items: center;
	}
}
.grade>div>div{
	// background-color: red;	
	width: 15vw;
}
#details {
	position: absolute;
	top: 290px;
	background-color: @var7;
	width: 90%;
	border-radius: 10px;
	box-shadow: 2px 5px 6px @var8;
	.title {
		color: black;
		font-weight: 900;
		font-size: 17px;
		margin: 1.5vh 4vw;
	}
	.list {
		padding: 0 4vw;
	}
	.item {
		display: flex;
		justify-content: space-between;
		border-bottom: 1px solid @var5;
		padding: 1vh 0;
		padding-top: 2vh;
		align-items: center;
	}
}

</style>
